const dbUtil = require('../util/dbutil')

// 查询用户信息
const getUserList = async (username = null, phone = null, name = null) => {
    if (username === null && phone == null && name == null) {
        return await dbUtil.getAll(`select * from user`)
    } else {
        return await dbUtil.getAll(`
   select * from user 
   where(username like concat('%',?,'%') or phone like concat('%',?) or name like concat('%',?,'%'))
   `, [username, phone, name])
    }

}


// 删除用户信息
const delUserList = async (id) => {
    const result = await dbUtil.exec(`DELETE FROM user WHERE id = ${id}`)

}

// 添加用户信息
const addUserList = async (username= null, password= null, phone= null, name= null, avatar = null, money= null) => {
    return await dbUtil.exec(`INSERT INTO user(username,password,phone,name,avatar,money) VALUES (?,?, ?, ?, ?, ?)`, [username, password, phone, name, avatar, money])
}

// 修改用户信息
const updateUserList = async (id,username= null, password= null, phone= null, name= null, avatar= null, money= null) => {

    return await dbUtil.exec(`UPDATE user SET username=?,password=?,phone=?,name=?,avatar=?,money=? where id = ?`, [username, password, phone, name, avatar, money, id])
}


module.exports = {
    getUserList,
    delUserList,
    addUserList,
    updateUserList
}